package flc.ast.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import flc.ast.R;
import flc.ast.activity.PairResultActivity;
import flc.ast.adapter.StarAdapter;
import flc.ast.bean.StarsBean;
import flc.ast.databinding.FragmentPairBinding;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link PairFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class PairFragment extends BaseNoModelFragment<FragmentPairBinding> {

    private StarAdapter starAdapter;
    private List<StarsBean> listStar = new ArrayList<>();
    private int type;
    private StarsBean yourStar, herStar;

    @Override
    protected int onCreate() {
        return R.layout.fragment_pair;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);

        mDataBinding.ivYourStarSignAdd.setOnClickListener(this);
        mDataBinding.ivYourStarSign.setOnClickListener(this);
        mDataBinding.ivHerStarSignAdd.setOnClickListener(this);
        mDataBinding.ivHerStarSign.setOnClickListener(this);
        mDataBinding.tvStartPair.setOnClickListener(this);

        mDataBinding.rvStarList.setLayoutManager(new GridLayoutManager(mContext, 3));
        starAdapter = new StarAdapter();
        mDataBinding.rvStarList.setAdapter(starAdapter);
        starAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivYourStarSignAdd://你的星座
            case R.id.ivYourStarSign:
                type = 0;
                break;
            case R.id.ivHerStarSignAdd://ta的星座
            case R.id.ivHerStarSign:
                type = 1;
                break;
            case R.id.tvStartPair://开始配对
                if (yourStar == null || herStar == null) {
                    ToastUtils.showShort(getString(R.string.please_sel_star));
                    return;
                }
                PairResultActivity.yourStar = yourStar;
                PairResultActivity.herStar = herStar;
                startActivity(PairResultActivity.class);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (type == 0) {
            yourStar = starAdapter.getItem(position);
            mDataBinding.ivYourStarSign.setImageResource(yourStar.getImg());
            mDataBinding.rlYourStarSignAdd.setVisibility(View.GONE);
            mDataBinding.rlYourStarSign.setVisibility(View.VISIBLE);
        } else if (type == 1) {
            herStar = starAdapter.getItem(position);
            mDataBinding.ivHerStarSign.setImageResource(herStar.getImg());
            mDataBinding.rlHerStarSignAdd.setVisibility(View.GONE);
            mDataBinding.rlHerStarSign.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void initData() {
        listStar.add(new StarsBean(getString(R.string.star_sign11), R.drawable.spz, getString(R.string.birth_date11)));
        listStar.add(new StarsBean(getString(R.string.star_sign12), R.drawable.syz, getString(R.string.birth_date12)));
        listStar.add(new StarsBean(getString(R.string.star_sign1), R.drawable.byz, getString(R.string.birth_date1)));
        listStar.add(new StarsBean(getString(R.string.star_sign2), R.drawable.jnz, getString(R.string.birth_date2)));
        listStar.add(new StarsBean(getString(R.string.star_sign3), R.drawable.shuangzz, getString(R.string.birth_date3)));
        listStar.add(new StarsBean(getString(R.string.star_sign4), R.drawable.jxz, getString(R.string.birth_date4)));
        listStar.add(new StarsBean(getString(R.string.star_sign5), R.drawable.szz, getString(R.string.birth_date5)));
        listStar.add(new StarsBean(getString(R.string.star_sign6), R.drawable.cnz, getString(R.string.birth_date6)));
        listStar.add(new StarsBean(getString(R.string.star_sign7), R.drawable.tcz, getString(R.string.birth_date7)));
        listStar.add(new StarsBean(getString(R.string.star_sign8), R.drawable.txz, getString(R.string.birth_date8)));
        listStar.add(new StarsBean(getString(R.string.star_sign9), R.drawable.ssz, getString(R.string.birth_date9)));
        listStar.add(new StarsBean(getString(R.string.star_sign10), R.drawable.mxz, getString(R.string.birth_date10)));
        starAdapter.setList(listStar);
    }
}